package com.example.duan;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {

    private TextView tv_so;
    private EditText et_so;
    private Button btn_sc,btn_empty,btn_copy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_so = findViewById(R.id.tv_so);
        et_so = findViewById(R.id.et_so);
        btn_sc = findViewById(R.id.btn_sc);
        btn_empty = findViewById(R.id.btn_empty);
        btn_copy = findViewById(R.id.btn_copy);

        btn_empty.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                et_so.setText(null);
                Toast.makeText(MainActivity.this, "清空了哦", Toast.LENGTH_SHORT).show();
            }
        });



        btn_sc.setOnClickListener(v -> {

            String btnsc = et_so.getText().toString();
            if (btnsc.isEmpty()) {
                Toast.makeText(this, "请输入要缩短的网址", Toast.LENGTH_SHORT).show();
                return;
            }

//            btn_sc.setEnabled(false);
//            btn_sc.setText("正在生成");

            String url = "http://api.985.so/api.php?format=json&url=" + btnsc + "&apikey=18775502577@a81c9c4533945fc107cb03028b4f5d39";

            OkGo.<String>get(url).execute(new StringCallback() {
                @Override
                public void onSuccess(Response<String> response) {
                    try {
                        JSONObject jsonObject = new JSONObject(response.body());
                        String success = jsonObject.getString("url");
                        String err = jsonObject.getString("err");
                        tv_so.setText(success+err);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    btn_sc.setEnabled(true);
                    btn_sc.setText("生成");
                }
            });


        });

        btn_copy.setOnClickListener(v -> {
            ClipboardManager copy = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            copy.setText(tv_so.getText());
            Toast.makeText(MainActivity.this,"复制成功",Toast.LENGTH_SHORT).show();
        });

      tv_so.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
              cm.setText(tv_so.getText());
              Toast.makeText(MainActivity.this,"复制成功",Toast.LENGTH_SHORT).show();


          }
      });
    }
}